<?php
namespace common\models\user;

use cms\modules\v1\models\user\User;
use common\models\Common;
use common\models\Functions;
use common\models\im\Im;
use common\models\Status;
use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class UserUnusualLog extends Common
{
    public $endDate = '';

    public static function tableName()
    {
        return '{{%user_unusual_log}}';
    }

    // 添加异常
    public static function disableUser($user_id = 0, $type = '', $duration = '', $reason = '') {

            if (empty($user_id)) {
                throw new Exception("参数错误");
            }
            $data['user_id'] = $user_id;
            $data['type'] = $type;
            $data['reason'] = $reason;
            $data['auto_normal_time'] = date('Y-m-d H:i:s',strtotime("+{$duration} days"));
            $data['handle_normal_time'] =  $data['auto_normal_time'];
            $data['create_time'] = date("Y-m-d H:i:s");
            $data['duration'] = $duration;
            $data['status'] = Status::USER_UNUSUAL_STATUS_UNUSUAL;
            $res = static::insertData($data);
            if ($res === false) {
                throw new Exception('异常处理失败');
            }
    }

    //封号提示
    public static function checkUserUnusual($userId = 0 ,$type = 0) {
        $where['user_id'] = $userId;
        $where['type'] = $type;
        $where['status'] = Status::USER_UNUSUAL_STATUS_UNUSUAL;
        $andWhere[] = [">", "handle_normal_time", date("Y-m-d H:i:s")];

        $info = static::getOneByAndWhere($where, $andWhere, "*", "handle_normal_time DESC");

        $typeName = '账号封禁中';

        if ($type != Status::USER_UNUSUAL_TYPE_ALL) {
            $typeName = Status::USER_UNUSUAL_TYPE_MAP[$type] ?? '';
        }


        if (!empty($info)) {
            throw new Exception("{$typeName}，解除时间：{$info['handle_normal_time']}");
        }
    }

    // 处理过期情况
    public function unblock()
    {
        $dateTime = date('Y-m-d H:i:s');
//        $dateTime = date('2022-10-30 17:26:17');
        $where['status'] = Status::USER_UNUSUAL_STATUS_UNUSUAL;
        $andWhere[] = ['<', 'auto_normal_time', $dateTime];
        $expireList = static::getByAndWhere($where, $andWhere);
        if (!empty($expireList)) {
            foreach ($expireList as $value) {
                $this->_unblock($value);
            }
        }
    }

    // 处理过期情况
    public function _unblock($value = []) {
        $trans = Yii::$app->getDb()->beginTransaction();
        try {
            //更新操作
            $data['handle_normal_time'] = date('Y-m-d H:i:s');
            $data['status'] = Status::USER_UNUSUAL_STATUS_NORMAL;
            $data['mark'] = '解封时间自动解封';
            $res = static::updateData($data, ['id' => $value['id']]);
            if ($res === false) {
                throw new Exception('系统错误，请联系开发小哥哥~~');
            }
            if($value['type'] == Status::USER_UNUSUAL_TYPE_ALL){
                User::updateData(['status' => Status::COMMON_RADIO_YES],['id' => $value['user_id']]);
            }

            if($value['type'] == Status::USER_UNUSUAL_TYPE_CHAT){
                $singUserInfo = \common\models\user\User::getOneInfoById($value['user_id'],['id','tx_code','login_name']);
                if ($singUserInfo) {
                    // 更新用户信息
                    $updateImUser['Tag_Profile_Custom_jinyan'] = (string) Status::COMMON_RADIO_NO;
                    $resIm = (new Im()) -> updateUser($singUserInfo['tx_code'], $updateImUser);
                    if (!$resIm) {
                        throw new Exception('更新用户信息失败');
                    }
                }
            }

            $trans->commit();
        } catch (Exception $e) {
            $trans->rollBack();
        }
    }

}
